package com.moyq5.spring.boot.app.register.server;

import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.support.MessageBuilder;

import com.moyq5.spring.boot.app.register.AppInfo;
import com.moyq5.spring.boot.app.register.server.AppInfoNotifyGateway.Gateway;
import com.moyq5.spring.boot.app.register.utils.JSON;

/**
 * @author Moyq5
 * @date 2019年3月6日
 */
public class AppInfoNotifierDefault implements AppInfoNotifier {
	
	private static final String TOPIC = "app/info/to/app";
	
	private Gateway gateway;
	
	public AppInfoNotifierDefault(Gateway gateway) {
		this.gateway = gateway;
	}
	
	@Override
	public void notice(AppInfo info) {
		String json = JSON.toString(info);
		MessageBuilder<String> builder = MessageBuilder.withPayload(json);
		builder.setHeader(MqttHeaders.TOPIC, TOPIC + "/" + info.getAppId());
		gateway.send(builder.build());
	}

}
